package com.mb.app.service.impl;

import com.mb.app.dao.IUserRegMapper;
import com.mb.app.dao.TbReportRegMapper;
import com.mb.app.domain.TbAppInfo;
import com.mb.app.domain.TbReportReg;
import com.mb.app.service.IUserRegService;
import com.mb.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 总流量统计服务实现
 * @author liliping
 * @create 2019-03-27 12:06 PM
 **/
@Service("userRegService")
public class UserRegServiceImpl implements IUserRegService {
    @Autowired
    private IUserRegMapper userRegMapper;

    @Autowired
    private TbReportRegMapper reportRegMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int runReport() {
        String reportDate = DateUtils.format(new Date(),"yyyy-MM-dd");

        Map<String,Object> map = new HashMap<>();
        map.put("reportDate", reportDate);
        List<TbAppInfo> list = userRegMapper.getAppInfo();
        if(list != null && list.size() != 0){
            Iterator<TbAppInfo> it = list.iterator();
            while (it.hasNext()){
                TbAppInfo appInfo = it.next();
                Long appId = appInfo.getAppId();
                map.put("appId", appId);

                Integer regCount = this.getRegCount(map);
                Integer authCount = this.getAuthCount(map);
                Integer contactAuthCount = this.getContactAuthCount(map);
                Integer bankCardAuthCount = this.getBankCardAuthCount(map);
                Integer reqLoanCount = this.getReqLoanCount(map);

                Map<String,Object> delMap = new HashMap<>();
                delMap.put("app_id",appId);
                delMap.put("report_date",reportDate);
                reportRegMapper.deleteByMap(delMap);

                TbReportReg reportReg = new TbReportReg();
                reportReg.setReportDate(reportDate);
                reportReg.setAppId(appId);
                reportReg.setRegCount(regCount);
                reportReg.setAuthCount(authCount);
                reportReg.setContactAuthCount(contactAuthCount);
                reportReg.setBankCardAuthCount(bankCardAuthCount);
                reportReg.setReqLoanCount(reqLoanCount);
                reportRegMapper.insert(reportReg);
            }
        }

        return 1;
    }

    private Integer getRegCount(Map<String,Object> map){
        return userRegMapper.getRegCount(map);
    }


    private Integer getAuthCount(Map<String,Object> map){
        return userRegMapper.getAuthCount(map);
    }

    private Integer getContactAuthCount(Map<String,Object> map){
        return userRegMapper.getContactAuthCount(map);
    }

    private Integer getBankCardAuthCount(Map<String,Object> map){
        return userRegMapper.getBankCardAuthCount(map);
    }

    private Integer getReqLoanCount(Map<String,Object> map){
        return userRegMapper.getReqLoanCount(map);
    }
}
